<?php
/**
 * 这个用类 用来管理 <用户>
 * 
 */
require_once (HOME . '/include/base/BackendController.php');
class employeeManager extends BackendController {
	/**
	 * 职工模型文件
	 */
	private $mod_employee;
	
	
	
	function __construct() {
		parent::__construct ();
		$this->employeeManager ();
	}
	function employeeManager() {
		$this->mod_employee = spClass('m_employee');
	}
	
	function index(){
		
		$result = $this->mod_employee->spLinker()->findAll();
		
// 		dump($result);
		
		$this->assign('employeeList', $result);		
		$this->assign('action', 'list');
		$this->display('employeeManager.layout.html');
	}
	/**
	 * 添加新用户
	 */
	function add() {
		if (! IS_POST) {
			
			$this->assign('action', 'add');
			$this->display('employeeManager.layout.html');
			
		} else {
			if ( $this->_check_post(array('username','realname','nickname')) ){
				
				$new_emp=array(
						'username' => $this->spArgs('username'),
						'realname' => $this->spArgs('realname'),
						'nickname' => $this->spArgs('nickname')						
						);
				
				$this->mod_employee->create($new_emp);
					
			}else{
					//TODO 数据不合法处理
				
			}
			
			//TODO验证用户的合法性 spVerify
		}
		// TODO 增加一个用户
	}
	/**
	 * 编辑已存在的用户
	 */
	function edit() {
		// TODO 编辑已存在的用户
	}
	
	function delete() {
		// TODO 删除已存在的用户
	}
	function ajax_tree(){
		$mod_department = spClass('m_department');
		$tree = array ();
		$dep_id = $this->spArgs ( 'dep_id' );
		if ($dep_id ) {
			$dep_result = $mod_department->spLinker ()->findAll ( "parent_id = {$dep_id}" );
			// 			dump ( $result );
			// 			json_encode($result);
		} else {
			$dep_result = $mod_department->spLinker ()->findAll ( 'parent_id = 0' );
		} // end of deparment sql 查询
		
// 		dump($dep_result);
		if ($dep_id ) {
			$for_emp_result = $mod_department->spLinker ()->find ( "department_id = {$dep_id}" );
		}else{
			$for_emp_result = $dep_result;
		}
		if (!empty( $for_emp_result['employee'] )){
			foreach ( $for_emp_result['employee'] as $emp_obj ){
				$tmp_obj = NULL;
				$tmp_obj['id'] = $emp_obj ['employee_id'];
				$tmp_obj['name'] = $emp_obj ['realname'];
				$tmp_obj['emp_name'] = $emp_obj ['realname'];
				$tmp_obj['isParent'] = FALSE;
				array_push($tree, $tmp_obj);
			}
		}
// 		dump($tree);
		/////////////////
		if (!empty ( $dep_result )) {
			foreach ( $dep_result as $i => $dep_obj ) {
				$tmp_obj = NULL;
				$tmp_obj	['id'] = $dep_obj ['department_id'];
				$tmp_obj ['name'] = $dep_obj ['name'];
				$tmp_obj ['dep_name'] = $dep_obj ['name'];
				$tmp_obj ['isParent'] = TRUE;
				array_push($tree, $tmp_obj);
// 				$tree [$i] ['department_id'] = "my HERO";
			}
		}
		if (empty($tree)){
			echo json_encode ( array (
					'error' => '下面没有数据了!!!'
			) );
		}else {
			
			echo json_encode ( $tree );
		}
		
	
		// 		var_dump ( $tree );
		
		// dump ( $result );
	}
	/**
	 * 编辑账户资料
	 */
	private function _editProfile() {
	}
}

?>